<?php
namespace app\flash\controller;
use QL\QueryList;
use think\Db;
use think\Controller;
use think\Common;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET','OPTIONS');
class Deepnessa extends Controller{
    /**
     * 功能：深度列表的采集接口
     * 时间：2019/08/13
     * 作者：张仕虎
     */
    public function gatherdeepness(){
        $url = "https://www.bishijie.com/shendu.html";
        $rulers = [
            //文章标题
            'title'   => ['.cTitle a','text'],
            'picture' => ['.cImg','src'],
            'source'  => ['.laiyuan','text'],
            'content' => ['.cRight p','text'],
            'id'      => ['.container>a','href'],
        ];
        $data = QueryList::Query($url,$rulers)->data;
        foreach ($data as $key => $v) {
            $data[$key]['content_id'] = substr($v['id'],8);  
            $data[$key]['title'] = \replace($v['title']);
            $data[$key]['title'] = str_replace('置顶原创','',$data[$key]['title']);    
           $arr = $this->details($v['id']);
             unset($data[$key]['id']);
             $data[$key]['content'] = json_encode($data[$key]['content']);
           $data[$key]['details'] = \json_encode($arr[0]['contentContainer']);
           $data[$key]['time'] = strtotime(date('Y-m-d H:i:s'));
        }
       
        $count = count($data);
        for ($i=0; $i <$count ; $i++) {
            $iscurrency = Db::name('deepness')->where(array('content_id'=>$data[$i]['content_id']))->find();
            if(empty($iscurrency)){
                //插入
                Db::name('deepness')->insertGetId($data[$i]);
            }else{
                //更新
                Db::name('deepness')->where(array('content_id'=>$data[$i]['content_id']))->update($data[$i]);
            }
        }
    }  
    public function details($id){
        $url = "https://m.bishijie.com".$id;
        $rulers = [
            'contentContainer' => ['.content','html'],
        ];
        $data = QueryList::Query($url,$rulers)->data;
        foreach ($data as $key => $value) {
            $str= preg_replace('/src="[^<>]+" data-src/', 'src', $data['0']['contentContainer']);
            $data[$key]['contentContainer'] = $str;
        }
       return $data;
    
    } 
    /**
     * 功能： 深度列表的查询
     * 时间： 2019/08/13
     * 作者： 张仕虎
     */
    public function selectdeepness(){
        $pagesize = input('pagesize')?input('pagesize'):20;
        $page     = input('page')?input('page'):1;
        $data = Db::table('deepness')->field('id,title,time,picture,source,content_id,pinglun_no')->order('content_id desc')->page($page)->paginate($pagesize,false,['query'=>request()->param()])->each(function($item,$key){
     $item['time'] = date('Y-m-d H:i:s',$item['time']);
     $item['title'] = str_replace('置顶原创','',$item['title']);
     //$item['count_data'] = Db::table('comment')->where(['message_id'=>$item['id'],'flag'=>2])->count();
    return $item;
    });
    return json(msg(1,$data,'查询成功'));
    }
    /**
     * 功能：深度列表的详情查询
     * 时间：2019/08/13
     * 作者：张仕虎
     */
    public function detailsdeepness(){
        $content_id = input('');
        $data = Db::table('deepness')->where(['content_id'=>$content_id])->field('id,title,details,source,picture,pinglun_no,time')->find();
        $data['details'] = json_decode($data['details'],true);
        $data['time']    = date('Y-m-d H:i:s',$data['time']);
        $data['title']   = str_replace('置顶原创','',$data['title']);
       if ($data) {
           return \json(msg(1,$data,'查询成功'));
       }else{
           return json(msg(0,'','查询失败'));
       }
    }
}